﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Tailor_Entity : EntityBase<Model.Tailor>
    {
        public override RESP_Info Add(Tailor model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TAILOR(TAILOR_ID,TAILOR_NAME,TAILOR_INFO,TAILOR_SPECIALTY,TAILOR_CASE,TAILOR_QQ,TAILOR_PHONE,TAILOR_HEAD_IMG,TAILOR_HOT) values (@TAILOR_ID,@TAILOR_NAME,@TAILOR_INFO,@TAILOR_SPECIALTY,@TAILOR_CASE,@TAILOR_QQ,@TAILOR_PHONE,@TAILOR_HEAD_IMG,@TAILOR_HOT)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", model.TAILOR_ID));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_NAME", model.TAILOR_NAME));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_INFO", model.TAILOR_INFO));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_SPECIALTY", model.TAILOR_SPECIALTY));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_CASE", model.TAILOR_CASE));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_QQ", model.TAILOR_QQ));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_PHONE", model.TAILOR_PHONE));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_HEAD_IMG", model.TAILOR_HEAD_IMG));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_HOT", model.TAILOR_HOT));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加定制师成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加定制师失败</p></h5>";
                    info.Url = "TailorAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加定制师失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }


        public override RESP_Info Update(Tailor model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TAILOR set TAILOR_NAME=@TAILOR_NAME,TAILOR_QQ=@TAILOR_QQ,TAILOR_PHONE=@TAILOR_PHONE,TAILOR_INFO=@TAILOR_INFO,TAILOR_SPECIALTY=@TAILOR_SPECIALTY,TAILOR_CASE=@TAILOR_CASE,TAILOR_HEAD_IMG=@TAILOR_HEAD_IMG,TAILOR_HOT=@TAILOR_HOT where TAILOR_ID=@TAILOR_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_NAME", model.TAILOR_NAME));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_QQ", model.TAILOR_QQ));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_PHONE", model.TAILOR_PHONE));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_INFO", model.TAILOR_INFO));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_SPECIALTY", model.TAILOR_SPECIALTY));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_CASE", model.TAILOR_CASE));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_HEAD_IMG", model.TAILOR_HEAD_IMG));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_HOT", model.TAILOR_HOT));
                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", model.TAILOR_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑定制师成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑定制师失败</p></h5>";
                    info.Url = "TailorList.aspx";

                    Tom.Net.Log.LogManager.Error("编辑定制师失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(Tailor model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TAILOR where TAILOR_ID=@TAILOR_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", model.TAILOR_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除定制师成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除定制师失败</p></h5>";
                    info.Url = "TailorList.aspx";

                    Tom.Net.Log.LogManager.Error("删除定制师失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override Tailor GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select TAILOR_ID,TAILOR_NAME,TAILOR_INFO,TAILOR_SPECIALTY,TAILOR_CASE,TAILOR_QQ,TAILOR_PHONE,TAILOR_HEAD_IMG,TAILOR_HOT from TAILOR where TAILOR_ID=@TAILOR_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Tailor tailor = new Tailor();
                        if (!reader.IsDBNull(0)) tailor.TAILOR_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tailor.TAILOR_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) tailor.TAILOR_INFO = reader.GetString(2);
                        if (!reader.IsDBNull(3)) tailor.TAILOR_SPECIALTY = reader.GetString(3);
                        if (!reader.IsDBNull(4)) tailor.TAILOR_CASE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) tailor.TAILOR_QQ = reader.GetString(5);
                        if (!reader.IsDBNull(6)) tailor.TAILOR_PHONE = reader.GetString(6);
                        if (!reader.IsDBNull(7)) tailor.TAILOR_HEAD_IMG = reader.GetString(7);
                        if (!reader.IsDBNull(8)) tailor.TAILOR_HOT = reader.GetInt16(8);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tailor;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override Tailor GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Tailor tailor = new Tailor();
                        if (!reader.IsDBNull(0)) tailor.TAILOR_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tailor.TAILOR_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) tailor.TAILOR_INFO = reader.GetString(2);
                        if (!reader.IsDBNull(3)) tailor.TAILOR_SPECIALTY = reader.GetString(3);
                        if (!reader.IsDBNull(4)) tailor.TAILOR_CASE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) tailor.TAILOR_QQ = reader.GetString(5);
                        if (!reader.IsDBNull(6)) tailor.TAILOR_PHONE = reader.GetString(6);
                        if (!reader.IsDBNull(7)) tailor.TAILOR_HEAD_IMG = reader.GetString(7);
                        if (!reader.IsDBNull(8)) tailor.TAILOR_HOT = reader.GetInt16(8);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tailor;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<Tailor> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();

                    List<Tailor> tailors = new List<Tailor>();
                    while (reader.Read())
                    {
                        Tailor tailor = new Tailor();
                        if (!reader.IsDBNull(0)) tailor.TAILOR_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tailor.TAILOR_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) tailor.TAILOR_INFO = reader.GetString(2);
                        if (!reader.IsDBNull(3)) tailor.TAILOR_SPECIALTY = reader.GetString(3);
                        if (!reader.IsDBNull(4)) tailor.TAILOR_CASE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) tailor.TAILOR_QQ = reader.GetString(5);
                        if (!reader.IsDBNull(6)) tailor.TAILOR_PHONE = reader.GetString(6);
                        if (!reader.IsDBNull(7)) tailor.TAILOR_HEAD_IMG = reader.GetString(7);
                        if (!reader.IsDBNull(8)) tailor.TAILOR_HOT = reader.GetInt16(8);
                        tailor.Tailor_IMG_List = new List<Tailor_IMG>();

                        using (SqlConnection conn_img = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
                        {
                            conn_img.Open();
                            SqlCommand cmd_img = new SqlCommand();
                            cmd_img.Connection = conn_img;
                            cmd_img.CommandType = System.Data.CommandType.Text;
                            cmd_img.CommandText = "select [PHOTO_ID],[PHOTO_URL] from [PHOTO] where  [PHOTO_ID] in (select photo_ID from [TAILOR_GALLERY]  where tailor_id = '" + tailor.TAILOR_ID + "')";
                            SqlDataReader reader_img = cmd_img.ExecuteReader();

                            while (reader_img.Read())
                            {
                                Tailor_IMG t_img = new Tailor_IMG();
                                t_img.Tailor_ID = tailor.TAILOR_ID;
                                if (!reader_img.IsDBNull(0)) t_img.PHOTO_ID = reader_img.GetString(0);
                                if (!reader_img.IsDBNull(1)) t_img.PHOTO_SRC = reader_img.GetString(1);
                                tailor.Tailor_IMG_List.Add(t_img);
                            }
                            reader_img.Close();
                            reader_img.Dispose();
                            cmd_img.Dispose();
                        }
                        tailors.Add(tailor);
                    }


                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return tailors;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
